/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.inventory.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.inventory.entity.HostLinks;
import com.thinkgem.jeesite.modules.inventory.service.HostLinksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 连接Controller
 * @author johnKee
 * @version 2016-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/inventory/hostLinks")
public class HostLinksController extends BaseController {

	@Autowired
	private HostLinksService hostLinksService;
	
	@ModelAttribute
	public HostLinks get(@RequestParam(required=false) String id) {
		HostLinks entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hostLinksService.get(id);
		}
		if (entity == null){
			entity = new HostLinks();
		}
		return entity;
	}

	@RequestMapping(value = "topoSkeleton")
	@ResponseBody
	public Map<String, Object> topoValues(){
		logger.debug("request top values");
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("sumCount", 0);
		resultMap.put("typeCount", 0);
		resultMap.put("linkCount", 0);
		resultMap.put("types", new ArrayList<Map<String, Object>>());
		resultMap.put("links", new ArrayList<Map<String, Object>>());
		List<HostLinks> linksList = hostLinksService.findList(new HostLinks());

		Map<String, Map<String, Object>> typeMap = new HashMap<>();
		Map<String, Object> hostMap = new HashMap<>();
		int hostIndex = 0;
		for(HostLinks hostLinks:linksList){
			Map<String, Object> typeValue = null;
			if(typeMap.containsKey(hostLinks.getFromHostType())){
				 typeValue = typeMap.get(hostLinks.getFromHostType());
			}else {
				typeValue = new HashMap<>();
				typeMap.put(hostLinks.getFromHostType(), typeValue);
				typeValue.put("type", hostLinks.getFromHostType());
				typeValue.put("hostCount",0);
				typeValue.put("linkCount",0);
				typeValue.put("hosts",new ArrayList<Map<String, Object>>());
				((List)resultMap.get("types")).add(typeValue);
				resultMap.put("typeCount",(Integer)resultMap.get("typeCount")+1);
			}
			if(hostMap.containsKey(hostLinks.getFromHostid())){
				//  已经处理过的主机
				typeValue.put("linkCount",(Integer)typeValue.get("linkCount")+1);
			}else {
				typeValue.put("hostCount",(Integer)typeValue.get("hostCount")+1);
				typeValue.put("linkCount",(Integer)typeValue.get("linkCount")+1);
				Map<String, Object> hostItem = new HashMap<>();
				hostItem.put("hindex", hostIndex++);
				hostItem.put("alias", hostLinks.getFromHostalias());
				hostItem.put("attr",hostLinks.getFromHostlinkattr());
				hostItem.put("name", hostLinks.getFromHostname());
				((List)typeValue.get("hosts")).add(hostItem);
				hostMap.put(hostLinks.getFromHostid(), hostItem);
				resultMap.put("sumCount", (Integer)resultMap.get("sumCount")+1);
			}

			if(typeMap.containsKey(hostLinks.getToHostType())){
				typeValue = typeMap.get(hostLinks.getToHostType());
			}else {
				typeValue = new HashMap<>();
				typeMap.put(hostLinks.getToHostType(), typeValue);
				typeValue.put("type", hostLinks.getToHostType());
				typeValue.put("hostCount", 0);
				typeValue.put("linkCount", 0);
				typeValue.put("hosts", new ArrayList<Map<String, Object>>());
				((List)resultMap.get("types")).add(typeValue);
				resultMap.put("typeCount",(Integer)resultMap.get("typeCount")+1);
			}
			if(hostMap.containsKey(hostLinks.getToHostid())){
				//  已经处理过的主机
				typeValue.put("linkCount",(Integer)typeValue.get("linkCount")+1);
			}else {
				typeValue.put("hostCount",(Integer)typeValue.get("hostCount")+1);
				typeValue.put("linkCount",(Integer)typeValue.get("linkCount")+1);
				Map<String, Object> hostItem = new HashMap<>();
				hostItem.put("hindex", hostIndex++);
				hostItem.put("alias", hostLinks.getToHostalias());
				hostItem.put("attr",hostLinks.getToHostlinkattr());
				hostItem.put("name", hostLinks.getToHostname());
				((List)typeValue.get("hosts")).add(hostItem);
				hostMap.put(hostLinks.getToHostid(), hostItem);
				resultMap.put("sumCount", (Integer)resultMap.get("sumCount")+1);
			}

			//{"fromHostindex":0,"toHostindex":7,"fromHostalias":"12312","toHostalias":"32412","linkAttr":"192.168.1.232"}
			Map<String, Object> linkItem = new HashMap<>();
			linkItem.put("fromHostindex",((Map)hostMap.get(hostLinks.getFromHostid())).get("hindex"));
			linkItem.put("toHostindex",((Map)hostMap.get(hostLinks.getToHostid())).get("hindex"));
			linkItem.put("fromHostalias", hostLinks.getFromHostalias());
			linkItem.put("toHostalias", hostLinks.getToHostalias());
			linkItem.put("linkAttr", hostLinks.getLinkAttr());
			((List) resultMap.get("links")).add(linkItem);
			resultMap.put("linkCount", (Integer)resultMap.get("linkCount")+1);
		}

		//String json="{\"sumCount\":10,\"typeCount\":5,\"linkCount\":13,\"types\":[{\"type\":\"type_windows\",\"hostCount\":2,\"linkCount\":4,\"hosts\":[{\"hindex\":0,\"alias\":\"12312\"},{\"hindex\":1,\"alias\":\"12412\"}]},{\"type\":\"type_linux\",\"hostCount\":4,\"linkCount\":6,\"hosts\":[{\"hindex\":2,\"alias\":\"22332\"},{\"hindex\":3,\"alias\":\"22422\"},{\"hindex\":4,\"alias\":\"22333\"},{\"hindex\":5,\"alias\":\"22423\"}]},{\"type\":\"type_network\",\"hostCount\":2,\"linkCount\":13,\"hosts\":[{\"hindex\":6,\"alias\":\"32312\"},{\"hindex\":7,\"alias\":\"32412\"}]},{\"type\":\"type_storage\",\"hostCount\":1,\"linkCount\":2,\"hosts\":[{\"hindex\":8,\"alias\":\"42312\"}]},{\"type\":\"type_others\",\"hostCount\":1,\"linkCount\":1,\"hosts\":[{\"hindex\":9,\"alias\":\"52312\"}]}],\"links\":[{\"fromHostindex\":0,\"toHostindex\":6,\"fromHostalias\":\"12312\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":0,\"toHostindex\":7,\"fromHostalias\":\"12312\",\"toHostalias\":\"32412\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":1,\"toHostindex\":6,\"fromHostalias\":\"12412\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":1,\"toHostindex\":7,\"fromHostalias\":\"12412\",\"toHostalias\":\"32412\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":2,\"toHostindex\":6,\"fromHostalias\":\"22332\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":2,\"toHostindex\":7,\"fromHostalias\":\"22332\",\"toHostalias\":\"32412\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":3,\"toHostindex\":6,\"fromHostalias\":\"22422\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":3,\"toHostindex\":7,\"fromHostalias\":\"22422\",\"toHostalias\":\"32412\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":4,\"toHostindex\":6,\"fromHostalias\":\"22333\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":5,\"toHostindex\":6,\"fromHostalias\":\"22423\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":8,\"toHostindex\":6,\"fromHostalias\":\"42312\",\"toHostalias\":\"32312\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":8,\"toHostindex\":7,\"fromHostalias\":\"42312\",\"toHostalias\":\"32412\",\"linkAttr\":\"192.168.1.232\"},{\"fromHostindex\":9,\"toHostindex\":7,\"fromHostalias\":\"52312\",\"toHostalias\":\"32412\",\"linkAttr\":\"192.168.1.232\"}]}";
		return resultMap;
	}


	@RequestMapping(value = "")
	public String hostTopo(HostLinks hostLinks, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/inventory/hostTopo";
	}

	@RequestMapping(value = "form")
	public String form(HostLinks hostLinks, Model model) {
		model.addAttribute("hostLinks", hostLinks);
		return "modules/inventory/hostLinksForm";
	}

	@RequestMapping(value = "save")
	public String save(HostLinks hostLinks, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hostLinks)){
			return form(hostLinks, model);
		}
		hostLinksService.save(hostLinks);
		addMessage(redirectAttributes, "保存主机连接成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/hostLinks/?repage";
	}
	

	@RequestMapping(value = "delete")
	public String delete(HostLinks hostLinks, RedirectAttributes redirectAttributes) {
		hostLinksService.delete(hostLinks);
		addMessage(redirectAttributes, "删除主机连接成功");
		return "redirect:"+Global.getAdminPath()+"/inventory/hostLinks/?repage";
	}

}